// 统一API入口文件
// 导出所有API服务和工具

// 导入环境配置
export { API_CONFIG, isDevelopment, isProduction, isTest } from '@/config/env'

// 导入HTTP客户端
export { default as http } from './http'

// 导入API工具
export {
  ApiResponse,
  PaginationBuilder,
  SearchBuilder,
  ApiErrorHandler,
  FileUploadHelper,
  ApiCache,
  paginationBuilder,
  searchBuilder,
  apiCache
} from './utils'

// 导入兼容层（保持与现有代码的兼容性）
export {
  menuApiCompatibility as menuApi,
  roleApiCompatibility as roleApi,
  userApiCompatibility as userApi,
  permissionApiCompatibility as permissionApi,
  operationLogApiCompatibility as operationLogApi,
  authApiCompatibility as authApi,
  personalApiCompatibility as personalApi,
  uploadApiCompatibility as uploadApi
} from './compatibility'

// 导入所有服务作为默认导出
import apiServices from './services'
import apiUtils from './utils'
import { API_CONFIG, isDevelopment, isProduction, isTest } from '@/config/env'
import http from './http'

// 创建统一的API对象
const api = {
  // 环境配置
  config: {
    API_CONFIG,
    isDevelopment,
    isProduction,
    isTest
  },
  
  // HTTP客户端
  http,
  
  // API服务
  ...apiServices,
  
  // API工具
  utils: apiUtils,
  
  // 便捷方法
  // 用户相关
  login: apiServices.auth.login,
  logout: apiServices.auth.logout,
  getUserInfo: apiServices.auth.getInfo,
  
  // 通用CRUD操作
  getList: (service, params) => service.getList(params),
  getById: (service, id) => service.getById(id),
  create: (service, data) => service.create(data),
  update: (service, id, data) => service.update(id, data),
  delete: (service, id) => service.delete(id),
  search: (service, keyword, params) => service.search(keyword, params),
  
  // 错误处理
  handleError: apiUtils.ApiErrorHandler.handle,
  withErrorHandling: apiUtils.ApiErrorHandler.withErrorHandling,
  
  // 文件上传
  uploadFile: apiServices.upload.uploadFile,
  uploadImage: apiServices.upload.uploadImage,
  
  // 缓存操作
  cache: {
    set: apiUtils.apiCache.set.bind(apiUtils.apiCache),
    get: apiUtils.apiCache.get.bind(apiUtils.apiCache),
    delete: apiUtils.apiCache.delete.bind(apiUtils.apiCache),
    clear: apiUtils.apiCache.clear.bind(apiUtils.apiCache),
    has: apiUtils.apiCache.has.bind(apiUtils.apiCache)
  },
  
  // 分页和搜索构建器
  pagination: apiUtils.paginationBuilder,
  search: apiUtils.searchBuilder
}

export default api